package se.statifyme.server.services;

import java.util.Collection;

import javax.servlet.http.HttpServletRequest;

import se.statifyme.server.AccessService;
import se.statifyme.server.entities.OwnedItem;
import se.statifyme.server.store.OwnedItemStore;

public abstract class OwnedItemServiceImpl<E extends OwnedItem> implements OwnedItemService<E>{
	
	@Override
	public E get(HttpServletRequest request, String id) {
		return getStore().get(id, getCurrentUserId(request));
	}

	@Override
	public Collection<E> getAll(HttpServletRequest request) {
		return getStore().getAll(getCurrentUserId(request));
	}

	@Override
	public void save(HttpServletRequest request, E item) {
		getStore().save(item, getCurrentUserId(request));
	}

	@Override
	public void delete(HttpServletRequest request, String id) {
		getStore().delete(id, getCurrentUserId(request));
	}
	
	protected String getCurrentUserId(HttpServletRequest request) {
		return AccessService.INSTANCE.getUserId(request);
	}
	
	protected abstract OwnedItemStore<E> getStore();

}
